/**
 * OPBillContentsTable.java
 * @author pkendzo
 * Email: pkendzo@msn.com
 * 2010
 */
package lu.med.praxiswahl.wahlumed.client.rechnungen.op.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableColumn;

import lu.med.praxiswahl.wahlumed.client.rechnungen.op.controller.OPBillTableSelectionListener;
import lu.med.praxiswahl.wahlumed.client.rechnungen.op.model.OPBillTableModel;
import lu.med.praxiswahl.wahlumed.client.utilities.ColumnWidthControler;
import lu.med.praxiswahl.wahlumed.client.utilities.UniCode;
import lu.med.praxiswahl.wahlumed.client.wclient.Main;
import lu.med.praxiswahl.wahlumed.client.wclient.PatientenSuchmaskeListener;


public class OPBillContentsTable extends JTable implements ActionListener,
	BillItemListener, PatientenSuchmaskeListener
{

    /**
     * 
     */
    private static final long serialVersionUID = 2598902332012015101L;
    private OPBillTableModel opModel;
    private ColumnWidthControler c;
    private TableColumn column;

    public OPBillContentsTable(OPBillTableModel opModel)
    {
	
	super(opModel);
	this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	this.selectionModel = getSelectionModel();
	this.setRowHeight(30);
	this.selectionModel
		.addListSelectionListener(new OPBillTableSelectionListener(
			opModel));
	this.setDefaultRenderer(Object.class, new OPBillCellRenderer(opModel));
	
	this.opModel = opModel;
	 

	this.addMouseListener(new MouseAdapter()
	{
	    @Override
	    public void mouseReleased(MouseEvent evt)
	    {
		if (evt.isPopupTrigger())
		{
		    showMenu(evt);
		}
	    }
	});


	column = null;

	c = new ColumnWidthControler(
		Main.COLUMNWIDTH_CONFIGURATION_FILE, "oprechnungtbl", this);

	for (int i = 0; i < 9; i++)
	{
	    column = this.getColumnModel().getColumn(i);
	    column.setPreferredWidth(c.getColumnWidth(i));
	}

    }

    @Override
    public void actionPerformed(ActionEvent arg0)
    {
	int selectedRow = this.getSelectedRow();
	
	if(selectedRow == -1)
	{
	    return;
	}
	
	Long id = null;
	
	id = this.opModel.getData().getDataRowID()[selectedRow];
	
	if(id != null)
	{
	    this.opModel.getData().deleteBillContentItem(id);
	    this.opModel.getData().retrieveBillDataFromDB();
	    this.opModel.fireTableDataChanged();
	    this.opModel.fireTableStructureChanged();
	}
	// model.deleteRow();

    }

    public void showMenu(MouseEvent evt)
    {
	JPopupMenu menu = new JPopupMenu();
	JMenuItem item = new JMenuItem("Eintrag l" + UniCode.getoe() + "schen");
	item.addActionListener(this);
	menu.add(item);
	menu.show(this, evt.getX(), evt.getY());
    }

    @Override
    public void modelChangeAppend(long billNumber)
    {
	this.opModel.getData().setRechnung_nummer(billNumber);
	this.opModel.getData().retrieveBillDataFromDB();
	this.opModel.fireTableDataChanged();
	this.opModel.fireTableStructureChanged();
	
	for (int i = 0; i < 9; i++)
	{
	    column = this.getColumnModel().getColumn(i);
	    column.setPreferredWidth(c.getColumnWidth(i));
	}

    }

    @Override
    public void modelChanged(String matricule)
    {
	this.opModel.getData().clearDataSet();
	this.opModel.fireTableDataChanged();
	this.opModel.fireTableStructureChanged();

	for (int i = 0; i < 9; i++)
	{
	    column = this.getColumnModel().getColumn(i);
	    column.setPreferredWidth(c.getColumnWidth(i));
	}
	
    }

    /**
     * @return the opModel
     */
    public OPBillTableModel getOpModel()
    {
        return opModel;
    }

    /**
     * @param opModel the opModel to set
     */
    public void setOpModel(OPBillTableModel opModel)
    {
        this.opModel = opModel;
    }
    
    

}
